<?php

namespace Home\Model;

use Think\Model;

class FilesModel extends Model {

    protected $tableName = 'pay';

    function upload($posts) {

        $code = $posts['code'];
        $seller_uid = $posts['seller_uid'];
        $pics = $posts['files'];
        $uid = $posts['uid'];
        if ($code) {
            $path = "uploads/" . $code . "/"; //上传路径 
        } else {
            $path = "uploads/times/" . date("Ymd") . "/"; //上传路径
        }

        if (!is_dir($path)) {
            mkdir($path, 0777);
        }
        $error_name = $error_size = $error_type = 0;
        $typeArr = array("jpg", "png", "gif", "jpeg"); //允许上传文件格式
        $size_max = 500000 * 1024; //最大上传文件大小
        foreach ($pics as $v) {
            $name = $v['name'];
            $size = $v['size'];
            $name_tmp = $v['tmp_name'];
            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
            if ($name == '') {
                $error_name ++;
            }
            if ($size > $size_max) {
                $error_size++;
            }

            if (!in_array($type, $typeArr)) {
                $error_type++;
            }
        }

        if ($error_name > 0) {
            echo json_encode(array("code" => "no_upload_pic", "result" => "请选择上传图片"));
            exit;
        }
        if ($error_size > 0) {
            echo json_encode(array("code" => "over_size", "result" => "图片大小已超过500000KB！"));
            exit;
        }
        if ($error_type > 0) {
            echo json_encode(array("code" => "wrong_types", "result" => "清上传图片格式的文件！"));
            exit;
        }
        $times_success = 0;
        foreach ($pics as $v) {
            $name = $v['name'];
            $size = $v['size'];
            $name_tmp = $v['tmp_name'];

            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
            //$pic_name = time() . rand(10000, 99999) . "." . $type; //文件名称
            $pic_name = time() . rand(10000, 99999) . ".jpg";
            $pic_url = $path . $pic_name; //上传后图片路径+名称
            if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
                $times_success++;
            }
        }
//        file_put_contents("tres.txt", json_encode($pics));
//        $pics = array_unique($pics);
        if ($times_success > 0) {

            if ($seller_uid > 0) {
                $image = new \Think\Image();
                $pic_water = $pic_url;
                $pic_original = $path . "original_" . $pic_name;

                $str = "猫翻卖家 ID " . $seller_uid . "";
                copy($pic_water, $pic_original);
                $img_info = getimagesize($pic_water);

                $width = $img_info[0];
                $font_size = getWaterFontSize($width);
                $water_img = getWaterImg($width);
                $image->open($pic_water)->water($water_img, \Think\Image::IMAGE_WATER_CENTER, 50)->text($str, 'Public/images/simhei.ttf', $font_size, '#000000', \Think\Image::IMAGE_WATER_CENTER, -20)->save($pic_water);
                $image->open($pic_water)->text($str, 'Public/images/simhei.ttf', $font_size, '#FFFFFF', \Think\Image::IMAGE_WATER_CENTER, -24)->save($pic_water);
            }
            $rs = array("code" => "0", "result" => "ok", "pic_url" => $pic_url);
            //判断类型
          $this->checkCode($code,$uid,$pic_url);
        } else {
            $rs = array("code" => "config", "result" => "上传出错，请稍候再试！");
        }
        return $rs;
    }

    function checkCode($code, $uid, $pic_url) {
        if ($code == 'avatar') {
            M("user")->where("id = " . $uid . "")->save(array("avatar" => $pic_url));
        }
    }

}
?>

